#include "playscene.h"
#include <QMenuBar>
#include <QAction>
#include <QPainter>
#include "mybutton.h"
#include <QTimer>
#include <QLabel>
#include "mycoin.h"
#include "dataconfig.h"
#include <QDebug>
#include <QPropertyAnimation>
#include <QSoundEffect>
// playscene::playscene(QWidget *parent)
//     : QMainWindow{parent}
// {}
playscene::playscene(int index){
    this->levalindex=index;
    this->setFixedSize(320,588);
    this->setWindowTitle("翻金币");
    QMenuBar *bar=this->menuBar();
    this->setMenuBar(bar);
    QMenu *startmenu=bar->addMenu("开始");
    QAction *quit=startmenu->addAction("退出");
    QSoundEffect *backsound = new QSoundEffect(this);
    backsound->setSource(QUrl::fromLocalFile(":/res/TapButtonSound.wav"));
    QSoundEffect *filpsound = new QSoundEffect(this);
    filpsound->setSource(QUrl::fromLocalFile(":/res/TapButtonSound.wav"));
    QSoundEffect *winsound = new QSoundEffect(this);
    winsound->setSource(QUrl::fromLocalFile(":/res/TapButtonSound.wav"));

    connect(quit,&QAction::triggered,[=](){
        this->close();
    });
    mybutton *closebtn=new mybutton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    closebtn->setParent(this);
    closebtn->move(this->width()-closebtn->width(),this->height()-closebtn->height());
    connect(closebtn,&mybutton::clicked,[=](){
        backsound->play();
        QTimer::singleShot(500,this,[=](){
            this->hide();
            emit this->chooseSceneBack();
        });
    });
    QLabel *label=new QLabel;
    label->setParent(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    label->setFont(font);
    QString str=QString("leavel:%1").arg(this->levalindex);
    label->setText(str);
    label->setGeometry(QRect(30,this->height()-50,120,50));

    QLabel* winLabel = new QLabel;
    QPixmap tmpPix;
    tmpPix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
    winLabel->setPixmap(tmpPix);
    winLabel->setParent(this);
    winLabel->move( (this->width() - tmpPix.width())*0.5 , -tmpPix.height());

    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            QLabel* label=new QLabel;
            label->setGeometry(0,0,50,50);
            label->setPixmap(QPixmap(":/res/BoardNode(1).png"));
            label->setParent(this);
            label->move(57+i*50,200+j*50);
            mycoin *coin=new mycoin(":/res/Coin0001.png");
            coin->setParent(this);
            coin->move(58+i*50,204+j*50);

        }
    }
    dataConfig config;
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            gameArray[i][j]=config.mData[this->levalindex][i][j];
            QString img;
            if(gameArray[i][j])  {
                img =":/res/Coin0001.png";
            }
            else img=":/res/Coin0008.png";
            mycoin *coin=new mycoin(img);
            coin->setParent(this);
            coin->move(58+i*50,204+j*50);
            coin->posX=i;
            coin->posY=j;
            coin->flag=gameArray[i][j];
            coinBtn[i][j] = coin;

            connect(coin,&mycoin::clicked,[=](){



                filpsound->play();

                qDebug() << "点击的位置： x = " <<  coin->posX << " y = " << coin->posY ;
                coin->changeFlag();
                gameArray[i][j] = gameArray[i][j] == 0 ? 1 : 0; //数组内部记录的标志同步修改
                QTimer::singleShot(300, this,[=](){
                    if(coin->posX+1 <=3)
                    {
                        coinBtn[coin->posX+1][coin->posY]->changeFlag();
                        gameArray[coin->posX+1][coin->posY] = gameArray[coin->posX+1][coin->posY]== 0 ? 1 : 0;
                    }
                    if(coin->posX-1>=0)
                    {
                        coinBtn[coin->posX-1][coin->posY]->changeFlag();
                        gameArray[coin->posX-1][coin->posY] = gameArray[coin->posX-1][coin->posY]== 0 ? 1 : 0;
                    }
                    if(coin->posY+1<=3)
                    {
                        coinBtn[coin->posX][coin->posY+1]->changeFlag();
                        gameArray[coin->posX][coin->posY+1] = gameArray[coin->posX+1][coin->posY]== 0 ? 1 : 0;
                    }
                    if(coin->posY-1>=0)
                    {
                        coinBtn[coin->posX][coin->posY-1]->changeFlag();
                        gameArray[coin->posX][coin->posY-1] = gameArray[coin->posX+1][coin->posY]== 0 ? 1 : 0;
                    }
                    this->isWin = true;
                    for(int i = 0 ; i < 4;i++)
                    {
                        for(int j = 0 ; j < 4; j++)
                        {
                            //qDebug() << coinBtn[i][j]->flag ;
                            if( coinBtn[i][j]->flag == false)
                            {
                                this->isWin = false;
                                break;
                            }
                        }
                    }
                    if(this->isWin)
                    {
                        winsound->play();

                        qDebug() << "胜利";
                        for(int i = 0 ; i < 4;i++)
                        {
                            for(int j = 0 ; j < 4; j++)
                            {
                                coinBtn[i][j]->isWin = true;
                            }
                        }
                        QPropertyAnimation * animation1 =  new QPropertyAnimation(winLabel,"geometry");
                        animation1->setDuration(1000);
                        animation1->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                        animation1->setEndValue(QRect(winLabel->x(),winLabel->y()+114,winLabel->width(),winLabel->height()));
                        animation1->setEasingCurve(QEasingCurve::OutBounce);
                        animation1->start();
                    }
                });

            });

        }
    }

}

void playscene::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    pix.load(":/res/Title.png");
    pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap(10,30,pix.width(),pix.height(),pix);

}
